package com.nuanshui.heatedloan.util.springdata;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import com.nuanshui.framework.utils.CommonUtil;
import com.nuanshui.heatedloan.extension.annotation.DefaultSort;
import com.nuanshui.heatedloan.extension.enums.SortType;
import com.nuanshui.heatedloan.extension.jpasearch.FieldSort;
import com.nuanshui.heatedloan.util.dwz.DwzPage;


/**
 * 将dwz的page转换为springdate所需的pageable
 */
public class PageUtils {

	/**
	 * 生成spring data JPA 对象 描述
	 * 
	 * @param page
	 * @return
	 */
	public static Pageable createPageable(final DwzPage page) {
		if (StringUtils.isNotBlank(page.getOrderField())) {
			List<Order> orders = new ArrayList<Sort.Order>();
			// 忽略大小写
			if (page.getOrderDirection().equalsIgnoreCase(
					DwzPage.ORDER_DIRECTION_ASC)) {
				Order order = new Order(Sort.Direction.ASC,
						page.getOrderField());
				orders.add(order);
			} else {
				Order order = new Order(Sort.Direction.DESC,
						page.getOrderField());
				orders.add(order);
			}
			if (page.hasSort()) {
				List<FieldSort> fieldSorts = page.getSortFields();
				for (FieldSort fieldSort : fieldSorts) {
					orders.add(new Order(fieldSort.getSortType(), fieldSort
							.getFieldName()));
				}
			}
			return new PageRequest(page.getPlainPageNum() - 1,
					page.getNumPerPage(), new Sort(orders));
		}

		return new PageRequest(page.getPlainPageNum() - 1, page.getNumPerPage());
	}

	/**
	 * 将springDataPage的属性注入page描述
	 * 
	 * @param page
	 * @param springDataPage
	 */
	public static void injectPageProperties(final DwzPage page,
			final org.springframework.data.domain.Page<?> springDataPage) {
		page.setTotalCount(springDataPage.getTotalElements());
	}

	/**
	 * 将count注入page描述
	 * 
	 * @param page
	 * @param springDataPage
	 */
	public static void injectPageProperties(final DwzPage page, final Long count) {
		page.setTotalCount(count);
	}

	/**
	 * 创建分页请求,包括需要排序的字段以及升降序要求
	 * 
	 * @param pageNumber
	 *            页数
	 * @param pagzSize
	 *            每页记录数
	 * @param sort
	 *            排序字段
	 * @param order
	 *            排序顺序
	 * @return Spring Data JPA分页请求类
	 */
	public static PageRequest buildPageRequest(final int pageNumber,
			final int pagzSize, final String sort, final String order) {
		Sort sortO = null;
		if ("desc".equals(order)) {
			sortO = new Sort(Direction.DESC, sort);
			return new PageRequest(pageNumber - 1, pagzSize, sortO);
		} else if ("asc".equals(order)) {
			sortO = new Sort(Direction.ASC, sort);
			return new PageRequest(pageNumber - 1, pagzSize, sortO);
		} else {
			return new PageRequest(pageNumber - 1, pagzSize);
		}

	}

	/**
	 * 创建分页请求,包括需要排序的字段以及升降序要求
	 */
	public static PageRequest buildPageRequest(final DwzPage page) {
		Sort sortO = null;
		if ("desc".equals(page.getOrderDirection())) {
			sortO = new Sort(Direction.DESC, page.getOrderDirection());
			return new PageRequest(page.getPageNum() - 1, page.getPrePage(),
					sortO);
		} else if ("asc".equals(page.getOrderDirection())) {
			sortO = new Sort(Direction.ASC, page.getOrderDirection());
			return new PageRequest(page.getPageNum() - 1, page.getPrePage(),
					sortO);
		} else {
			return new PageRequest(page.getPageNum() - 1, page.getPrePage());
		}

	}

	public static void createDefaultSort(final DwzPage page, final Object params) {
		Class clazz = params.getClass();
		Field fields[] = clazz.getDeclaredFields();
		for (Field field : fields) {
			/*
			 * 如果指定此字段为默认排序方式
			 */
			if (field.isAnnotationPresent(DefaultSort.class)) {
				DefaultSort defaultSort = field
						.getAnnotation(DefaultSort.class);
				String fileName = defaultSort.fileName();
				String sort = "";
				if (CommonUtil.isEmpty(fileName)) {
					fileName = field.getName();
				}
				if (SortType.ASC.equals(defaultSort.sort())) {
					sort = DwzPage.ORDER_DIRECTION_ASC;
				} else if (SortType.DESC.equals(defaultSort.sort())) {
					sort = DwzPage.ORDER_DIRECTION_DESC;
				}
				page.setOrderField(fileName);
				page.setOrderDirection(sort);

			}
		}
	}

}
